<?php
/**
 * Lock class, lock/unlock doors
 * @package action
 * @author alcaitiff
 */
class Lock implements Action{
    /**
     * @static
     * @param Command $command Command
     * @param int $tick Tick
     * @return string
     */
    public static function run($command, $tick) {
        //Get the char room
        $parameters=$command->parameters;
        $line=$command->command;
        $char=$command->character;
		$room=$char->getRoom();
        if(is_array($parameters) && isset($parameters[0]) && $parameters[0]!=null){
            $direction = $parameters[0];
        }else{
            MessageSystem::addErrorMessage('You must specify a direction');
            return null;
        }

		$passage=$room->getPassageByDirection($direction);
		if($passage!=null){
            if($passage->door->isClean()){
                MessageSystem::addErrorMessage('No door available to ' . $direction);
                return null;
            }else{
                if($line=='lock'){
                    if($passage->door->isLocked()){
                        MessageSystem::addErrorMessage('This direction is already locked');
                        return null;
                    }else{
                        $passage->lock($char);
                        return $passage;
                    }
                }else{
                    if($passage->door->isLocked()){
                        $passage->unlock($char);
                        return $passage;
                    }else{
                        MessageSystem::addErrorMessage('This direction is not locked');
                        return null;
                    }
                }
            }
		}else{
			MessageSystem::addErrorMessage('No exit available to '.$direction);
            return null;
		}
    }
}